package com.dulichdanang.location.location;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import com.dulichdanang.R;
import com.dulichdanang.facebook.android.DialogError;
import com.dulichdanang.facebook.android.Facebook;
import com.dulichdanang.facebook.android.Facebook.DialogListener;
import com.dulichdanang.facebook.android.FacebookError;
import com.dulichdanang.facebook.android.Util;
import com.dulichdanang.user.MyUser;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LocationDetailActivity extends Activity {

	Facebook fb;
	ImageButton btnUser;TextView tvFacebookName;
	EditText txtComment;
	Button btnCommnet;
	ImageView imgAvatar;
	ListView lvComment;
	MyUser user;
	SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location_detail);
		Bundle re = this.getIntent().getExtras();
	    //  format : id-locationName
		String[] temp = (re.getString("data")).split("-");
		this.setTitle(temp[1]);
		//------Load comment---------------------------
		lvComment = (ListView)findViewById(R.id.lvComment);
		String[] strarr = new String[] {"Đình Trọng :\nThe user sees a list of items and can scroll through them",
				"Đình Trọng :\nThe user sees a list of items and can scroll through them",
				"Đình Trọng :\nThe user sees a list of items and can scroll through them",
				"Đình Trọng :\nThe user sees a list of items and can scroll through them"};
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1,strarr);
		
		lvComment.setAdapter(adapter);
		//---Facebook----------------------------------
		fb = new Facebook(getString(R.string.face_key));
		// Save login
		sp = getPreferences(MODE_PRIVATE);
		String access_token = sp.getString("access_token", null);
		long expires = sp.getLong("access_expires", 0);
		if(access_token != null){
			fb.setAccessToken(access_token);
		}
		if(expires != 0){
			fb.setAccessExpires(expires);
		}
		btnUser = (ImageButton)findViewById(R.id.btnUser);
		imgAvatar = (ImageView)findViewById(R.id.imgAvatar);
		tvFacebookName = (TextView)findViewById(R.id.tvFacebookName);
		txtComment = (EditText)findViewById(R.id.etComment);
		btnCommnet = (Button)findViewById(R.id.btnComment);
		checkLogin();
		user = new MyUser();
		OnClickListener btnUserClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!fb.isSessionValid()){
					fb.authorize(LocationDetailActivity.this, new DialogListener() {
						
						@Override
						public void onFacebookError(FacebookError e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onError(DialogError e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onComplete(Bundle values) {
							// TODO Auto-generated method stub
							Editor ed = sp.edit();
							ed.putString("access_token",fb.getAccessToken());
							ed.putLong("access_expires", fb.getAccessExpires());
							ed.commit();
							checkLogin();
						}
						
						@Override
						public void onCancel() {
							// TODO Auto-generated method stub
							
						}
					});
				}else {
					try {//
						fb.logout(getApplicationContext());
						checkLogin();
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		};
		OnClickListener btnCommentClikc = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//post
				Bundle parame = new Bundle();// caption, link
				parame.putString("name","Đình Trọng đã bình luận về 1 địa điểm Du dịch Đà Nẵng");
				parame.putString("description", "Ồ thật là đẹp, đẹp quá đẹp quá đẹp quá");
				parame.putString("link","http://co-mit.com/vi/");
				parame.putString("picture", "https://vtcdn.com/sites/all/themes/ttcn_istar/logo.png");
				fb.dialog(LocationDetailActivity.this, "feed",parame,new DialogListener() {
					
					@Override
					public void onFacebookError(FacebookError e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onError(DialogError e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onComplete(Bundle values) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onCancel() {
						// TODO Auto-generated method stub
						
					}
				}); 
			}
		};
		btnUser.setOnClickListener(btnUserClick);
		btnCommnet.setOnClickListener(btnCommentClikc);
		
	}
	private void checkLogin(){
		if(fb.isSessionValid()){
			btnUser.setImageResource(R.drawable.fb_logout);
			JSONObject obj = null;
			URL image_url = null;
			String JsonUser;
			try {
				JsonUser = fb.request("me");
				obj = Util.parseJson(JsonUser);
				String id = obj.optString("id");
				String name = obj.optString("name");
				tvFacebookName.setText("Xin chào,  "+name);
				imgAvatar.setVisibility(ImageView.VISIBLE);
				Log.e("image", "hehe");
				image_url = new URL("http://graph.facebook.com/"+id+"/picture?type=small");
				Bitmap bitmap = BitmapFactory.decodeStream(image_url.openConnection().getInputStream());
				imgAvatar.setImageBitmap(bitmap);
				btnCommnet.setEnabled(true);
				txtComment.setEnabled(true);
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FacebookError e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			tvFacebookName.setText("");
			btnUser.setImageResource(R.drawable.fb_login);
			imgAvatar.setVisibility(ImageView.INVISIBLE);
			btnCommnet.setEnabled(false);
			txtComment.setEnabled(false);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_location_detail, menu);
		return true;
	}

}
